package com.shanghaionstar.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Base64;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.HeadImgRes;
import com.onstar.cn.more.response.UploadImgRes;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.user.AccountInfoFragment;
import com.shanghaionstar.user.FeedBackFragment;
import com.shanghaionstar.user.WithActionSheetActivity;
import com.shanghaionstar.user.iview.IMoreUpLoadImg;
import com.shanghaionstar.user.presenter.MoreUpLoadImgPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.FinalConstants;

import java.io.ByteArrayOutputStream;

/**
 * Created by 7828wc on 2016/6/29.
 */
@ContentView(com.onstar.cn.framework.R.layout.fragment_container)
public class MoreActivity extends WithActionSheetActivity implements  IMoreUpLoadImg {
    private LogManager logger = LogManager.getInstance();
    public  FragmentManager        fragmentManager;
    private String                 plateNumber;
    private String                 engineNumber;
    private String                 licenseExpireDate;
    private String                 phoneNumber;
    private String                 emailAddress;
    /**
     * 当前保险公司
     */
    private String                 insuranceComp;
    /**
     * 交强险到期日
     */
    private String                 compulsoryInsuranceExpireDate;
    /**
     * 商业险到期日
     */
    private String                 businessInsuranceExpireDate;
    private MoreUpLoadImgPresenter presenter;
    private AccountInfoFragment    mInfoFragment;
    private boolean isFirst = true;
    private String fragmentFlag;
    private String oldFragmentFlag;
    private boolean isRestarted=false;

    private String contactAddress[];
    private String province;
    private String city;
    private String address;
    private String code;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new MoreUpLoadImgPresenter(getScheduler());
        presenter.setView(this);
        fragmentManager = getSupportFragmentManager();
        fragmentFlag = getIntent().getStringExtra(FinalConstants.JUMP_FLAG);
        if (!TextUtils.isEmpty(fragmentFlag)) {
            changeFragment(fragmentFlag);
        }
    }





    @Override
    protected void onResume() {
        super.onResume();
      /*  fragmentFlag = getIntent().getStringExtra(FinalConstants.JUMP_FLAG);
        oldFragmentFlag=fragmentFlag;
        changeFragment(fragmentFlag);*/

/*            if (!TextUtils.isEmpty(fragmentFlag)) {
                if(isFirst) {
                    changeFragment(fragmentFlag);
                    isFirst=false;
                }else {
                    String  fragmentFlag2 = getIntent().getStringExtra(FinalConstants.JUMP_FLAG);
                    if (!TextUtils.equals(oldFragmentFlag,fragmentFlag2)){
                        changeFragment(fragmentFlag2);
                        oldFragmentFlag=fragmentFlag2;
                    } else {
                        int N = fragmentManager.getBackStackEntryCount();
                        if (N < 1) {
                            finish();
                        }
                    }
                }
            } else {
                int N = fragmentManager.getBackStackEntryCount();
                if (N < 1) {
                    finish();
                }
            }*/
//        if(!isRestarted) {
//            fragmentFlag = getIntent().getStringExtra(FinalConstants.JUMP_FLAG);
//            if (!TextUtils.isEmpty(fragmentFlag)) {
//                changeFragment(fragmentFlag);
//            }
//        }else {
//            isRestarted=false;
//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }


    @Override
    protected void processSelectedPhoto(Bitmap bitmap, String extension) {
        mInfoFragment = (AccountInfoFragment) getFragment(AccountInfoFragment.class);
        mInfoFragment.displayHeadImg(bitmap);
        byte[] bytes = bitmap2Bytes(bitmap);
        String bitmapBytes = Base64.encode(bytes);
        //    uploadImg(bitmapBytes, extension, infoFragment);
        presenter.getData(bitmapBytes, extension);
    }

    public byte[] bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    @Override
    protected void processSelectedPhoto(String path) {
        FeedBackFragment feedBackFragment = (FeedBackFragment) getFragment(FeedBackFragment.class);
        feedBackFragment.processSelectedPhoto(path);
    }

    public void changeFragment(String fragmentFlag) {
        try {
            Class<?> clazz = Class.forName(fragmentFlag);
            Fragment fragment = (Fragment) clazz.newInstance();
            if (!CommonUtil.isNull(getIntent().getExtras())) {
                fragment.setArguments(getIntent().getExtras());
            }
            changeFragment(fragment, clazz, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        if (fragmentManager.getBackStackEntryCount() == 1) {
            this.finish();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public void setLoading(boolean isLoading) {
        AccountInfoFragment infoFragment = new AccountInfoFragment();
        if (isLoading) {
            mInfoFragment.showProgress(true);
        } else {
            mInfoFragment.showProgress(false);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByExceptionCode(MoreActivity.this, (HttpException) e);
    }

    @Override
    public void onReponseSuccess(UploadImgRes object) {
        if (object != null) {
            String des = object.getDescription();
            String code = object.getCode();
            if (code != null && code.equalsIgnoreCase("E0000")) {
                ToastView toast = new ToastView(MoreActivity.this);
                toast.showToast(getString(R.string.option_upload_success));
                OnstarApplication application = OnstarApplication.getApplication();
                application.setHeadImgRes(new HeadImgRes());
            } else {
                ToastView toast = new ToastView(MoreActivity.this);
                toast.showToast(getString(R.string.option_upload_failed));
            }
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        if (!CommonUtil.isNull(plateNumber))
            this.plateNumber = plateNumber;
    }

    public String getEngineNumber() {
        return engineNumber;
    }

    public void setEngineNumber(String engineNumber) {
        if (!CommonUtil.isNull(engineNumber))
            this.engineNumber = engineNumber;
    }

    public String getLicenseExpireDate() {
        return licenseExpireDate;
    }

    public void setLicenseExpireDate(String licenseExpireDate) {
        if (!CommonUtil.isNull(licenseExpireDate))
            this.licenseExpireDate = licenseExpireDate;
    }

    public String getInsuranceComp() {
        return insuranceComp;
    }

    public void setInsuranceComp(String insuranceComp) {
        if (!CommonUtil.isNull(insuranceComp))
            this.insuranceComp = insuranceComp;
    }

    public String getCompulsoryInsuranceExpireDate() {
        return compulsoryInsuranceExpireDate;
    }

    public void setCompulsoryInsuranceExpireDate(String compulsoryInsuranceExpireDate) {
        if (!CommonUtil.isNull(compulsoryInsuranceExpireDate))
            this.compulsoryInsuranceExpireDate = compulsoryInsuranceExpireDate;
    }

    public String getBusinessInsuranceExpireDate() {
        return businessInsuranceExpireDate;
    }

    public void setBusinessInsuranceExpireDate(String businessInsuranceExpireDate) {
        if (!CommonUtil.isNull(businessInsuranceExpireDate))
            this.businessInsuranceExpireDate = businessInsuranceExpireDate;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        if (!CommonUtil.isNull(phoneNumber))
            this.phoneNumber = phoneNumber;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        if (!CommonUtil.isNull(emailAddress))
            this.emailAddress = emailAddress;
    }

    public String[] getContactAddress() {
        return contactAddress;
    }

    public void setContactAddress(String[] contactAddress) {
        this.contactAddress = contactAddress;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        this.setIntent(intent);
        fragmentFlag = getIntent().getStringExtra(FinalConstants.JUMP_FLAG);
        if (!TextUtils.isEmpty(fragmentFlag)) {
            changeFragment(fragmentFlag);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        isRestarted=true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
